package org.znxs.comfriend.utils;

import org.znxs.comfriend.models.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 单例模式 存储全局数据
 */
public class AllUserList {
    private static AllUserList instance;
    private List<User> sharedList;

    private AllUserList() {
        sharedList = new ArrayList<>(); // 初始化为空列表
    }

    public static synchronized AllUserList getInstance() {
        if (instance == null) {
            instance = new AllUserList();
        }
        return instance;
    }

    public List<User> getSharedList() {
        return sharedList;
    }

    public void setSharedList(List<User> sharedList) {
        this.sharedList = sharedList;
    }

    public User getUserById(String userId) {
        List<User> collect = sharedList.stream().filter(item -> Objects.equals(item.getId(), userId)).collect(Collectors.toList());
        return collect.get(0);
    }
}